<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <el-form :inline="true">
          <el-form-item label="买家:">
            <h-user-choose v-model="search.userId"/>
          </el-form-item>
          <el-form-item label="订单日期:">
            <h-date-range-picker v-model="search.date"></h-date-range-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchAction"><i class="fa fa-search"></i> 查询</el-button>
          </el-form-item>

          <el-form-item label="退款状态:">
            <el-radio-group v-model="search.returned" @change="refundChange">
              <el-radio :label="0">未完成退款</el-radio>
              <el-radio :label="1">已完成退款</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div class="row mt-15">
      <div class="col">
        <el-table
          :data="datas"
          border
          tooltip-effect="dark"
          v-loading="loading"
          @selection-change="handleSelectionChange"

        >
          <el-table-column
            type="selection"
            width="48" align="center">
          </el-table-column>
          <el-table-column type="expand" label="详情">
            <template slot-scope="props">
              <div class="expand-div">
              <el-table
                :data="props.row.orders"
                tooltip-effect="dark"
                border
                stripe
              >
                <el-table-column
                  label="订单编号"
                  width="120" prop="sn">
                </el-table-column>
                <el-table-column
                  label="商品信息"
                  min-width="280">
                  <template slot-scope="scope">
                    <h-good-info-display :admin="true" v-model="scope.row"></h-good-info-display>
                  </template>
                </el-table-column>
                <el-table-column
                  label="商品规格"
                  min-width="100"
                >
                  <template slot-scope="scope">
                    <h-specs-display v-model="scope.row"></h-specs-display>
                  </template>
                </el-table-column>

                <el-table-column label="订单信息" min-width="200">
                  <template slot-scope="scope">
                    <h-order-info-display v-model="scope.row"></h-order-info-display>
                  </template>
                </el-table-column>
                <el-table-column
                  label="订单金额"
                  min-width="150">
                  <template slot-scope="scope">
                    <h-order-pay-info-display v-model="scope.row"/>
                  </template>
                </el-table-column>
                <el-table-column
                  label="状态信息" width="120" align="center">
                  <template slot-scope="scope">
                    <h-order-status-display v-model="scope.row" :apply="true" :dispatch="true" :arrival="true"
                                            :pay="true"/>
                  </template>
                </el-table-column>
              </el-table>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="sn"
            label="退款单号"
            width="120">
          </el-table-column>
          <el-table-column
            prop="nickName"
            label="买家信息"
            width="200">
            <template slot-scope="scope">
              <h-user-display v-model="scope.row.user"></h-user-display>
            </template>
          </el-table-column>
          <el-table-column
            label="创建日期"
            width="160" prop="createTime">
          </el-table-column>

          <el-table-column
            label="退款类型"
            width="150">
            <template slot-scope="scope">
            <span v-if="scope.row.type==1" class="red">
              退回余额
            </span>
              <span v-else-if="scope.row.type==2" class="red">
              原路退回
            </span>
              <span v-else class="red">
              退回指定账户
            </span>
            </template>
          </el-table-column>
          <el-table-column
            label="收款账户" prop="account"
          >
          </el-table-column>
          <el-table-column
            label="退款总数"
          >
            <template slot-scope="scope">
              <span class="red">{{scope.row.returnNum}}</span>
            </template>
          </el-table-column>
          <el-table-column label="退款总额">
            <template slot-scope="scope">
              <span class="red">￥{{scope.row.returnPrice}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="退款原因"
            prop="reason">
          </el-table-column>

          <el-table-column
            label="操作" align="center" width="140" fixed="right">
            <template slot-scope="scope">
              <el-tooltip placement="top" v-if="scope.row.returned==0">
                <div slot="content">退款确认操作</div>
                <el-button type="text" @click="doReturned(scope.$index,scope.row)">
                  确认退款
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <div class="h-pagination">
          <div v-if="search.returned==0" class="pagination-tool">
            <el-button type="primary" @click="batchReturned" v-loading="returnloading">批量确认退款</el-button>
          </div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import HDateRangePicker from 'src/components/common/HDateRangePicker'
  import HUserChoose from 'src/components/common/HUserChoose'
  import AdminService from 'src/service/admin'
  import HUserDisplay from 'src/components/common/display/HUserDisplay'
  import HOrderStatusDisplay from 'src/components/common/display/HOrderStatusDisplay'
  import HSpecsDisplay from 'src/components/common/display/HSpecsDisplay'
  import TableMix from 'src/components/table-mix'
  import HExpressDialog from 'src/components/admin/common/HExpressDialog'
  import HGoodInfoDisplay from 'src/components/common/display/HGoodInfoDisplay'
  import HOrderInfoDisplay from 'src/components/common/display/HOrderInfoDisplay'
  import HOrderPayInfoDisplay from 'src/components/common/display/HOrderPayInfoDisplay'

  export default {
    name: 'AdminOrderRefund',
    components: {
      HExpressDialog, HDateRangePicker, HOrderPayInfoDisplay,
      HUserChoose, HUserDisplay, HOrderStatusDisplay, HSpecsDisplay, HGoodInfoDisplay, HOrderInfoDisplay
    },
    mixins: [TableMix],
    data() {
      return {
        datas: [],
        search: {
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          needDetails: true,
          type: null,
          returned: 0,
          sn: null,
          dates: [],
          userId:null
        },
        selecteds: [],
        returnloading: false,
        loading: false,
      }
    },
    methods: {
      searchAction: function () {
        this.search.pagination.page = 1
        this.getDatas()
      },
      refundChange: function (v) {
        if (v == 0) {
          this.search.returned = 0
        } else if (v == 1) {
          this.search.returned = 1
        }
        this.searchAction()
      },
      doReturned: function (index, data) {
        const params = {
          ids: [data.id],
          status: 1
        }
        this.doneReturned(params)
      },
      doneReturned: function (data) {
        const me = this
        me.$confirm('此操作后将按用户要求把钱退回给用户，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          AdminService.doneRefund(data).then(ds => {
            if (ds != null) {
              me.$message.success('确认退款信息成功。')
              me.searchAction()
            } else {
              me.$message.error('确认退款信息失败!')
            }
          })
        }).catch(() => {
        })
      },
      batchReturned: function () {
        if (this.selecteds.length < 1) {
          this.$message.warning('请选择要确认退款的数据项。')
          return
        }
        const ids = this.selecteds.map(d => {
          return d.id
        })
        this.doneReturned({ids: ids, status: 1})
      },
      handleSelectionChange: function (v) {
        this.selecteds = v
      },
      getDatas: function () {
        const me = this
        me.loading = true
        AdminService.searchRefund(me.search).then(ds => {
          if (ds != null) {
            me.datas = ds.datas
            me.search.pagination = ds.pagination
          }
          me.loading = false
        })
      },
      handleSizeChange: function (size) {
        this.search.pagination.limit = size
        this.search.pagination.page = 1
        this.getDatas()
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page
        this.getDatas()
      }
    },
    mounted: function () {
      //this.initTalbeHeight(260)
      this.getDatas()
    }
  }
</script>
